$(document).ready(function(){
	cw.common.setStyle();
	cw.common.getUserSession(getUserInfo);
	$('#subImg').bind('click',function(){cw.common.getUserSession(toUpdateUserInfo)});
})

function getUserInfo(userinfo){
	$('#USER_ID').attr('value',userinfo.USER_ID);
	$('#LOGIN_NAME').html(userinfo.LOGIN_NAME);
	$('#STR_PHONE').html(userinfo.PHONE);
	$('#STR_USER_NAME').html(userinfo.USER_NAME);
	$('#STR_EMAIL').html(userinfo.EMAIL);
	$('#STR_REMARK').html(userinfo.REMARK);
}

function toUpdateUserInfo(userinfo){
	$('#subImg').attr('src','../images/anniu3.gif');
	
	$('#USER_NAME').attr('value',userinfo.USER_NAME);
	$('#USER_NAME').show();
	$('#STR_USER_NAME').hide();
	
	$('#PHONE').attr('value',userinfo.PHONE);
	$('#PHONE').show();
	$('#STR_PHONE').hide();
	
	$('#EMAIL').attr('value',userinfo.EMAIL);
	$('#EMAIL').show();
	$('#STR_EMAIL').hide();
	
	$('#REMARK').attr('value',userinfo.REMARK);
	$('#REMARK').show();
	$('#STR_REMARK').hide();
	
	$('#subImg').bind('click',subButton);
	validate();
}

//提交表单
function subFrm(){
	var options = {
	    dataType: 'json',
    	iframe:	false,
	    success: function(data) {
	    	if(data.success){
	    		alert('修改成功！');
	      		window.location.href="userinfo.html";
	      	}else{
	      		alert(data.errors.errmsg);
	      	}
	   	}
	};
	$.ajaxSetup({contentType:'application/x-www-form-urlencoded;charset=utf-8'});
    $('#frm').ajaxSubmit(options);
}

//提交验证
function subButton(){
	return jQuery.formValidator.pageIsValid(1);
}

//验证
function validate(){
	$.formValidator.initConfig({formid:"frm",onerror:function(msg){},onsuccess:function(){subFrm();return false;}});
	$("#USER_NAME").formValidator({onshow:"请输入真实姓名",oncorrect:"正确",onfocus:"请输入您的真实姓名"}).inputValidator({min:1,onerror:"真实姓名必填"}).inputValidator({max:10,onerror:"真实姓名中文不超过5个字，英文不超过10个字符"})
	.functionValidator({
		fun:function (str,elem){
			var a = $.trim(str);
			if(a.length == 0) return false;
			return true;
		}, onerror: "真实姓名必填"
	})
	$("#PHONE").formValidator({empty:true,onshow:"请输入联系号码",oncorrect:"正确",onfocus:"号码可以是手机或者固话"}).inputValidator({min:1,onerror:"请输入联系号码",max:20,onerror:"字数不超过20个字符"});
	$("#EMAIL").formValidator({empty:true,onshow:"请输入有效的邮箱地址",oncorrect:"正确",onfocus:"请输入有效的E-mail地址"})
	.inputValidator({min:1,onerror:"请输入邮箱地址"}).inputValidator({max:60,onerror:"邮箱名称过长"})
	.functionValidator({fun:isValidEmail,onerror:"邮箱格式不正确"});
	$("#REMARK").formValidator({empty:true,onshow:"请输入用户备注",oncorrect:"正确",onfocus:"请输入用户备注信息"}).inputValidator({max:1000,onerror:"用户备注信息中文不超过500个字，英文不超过1000个字符"});
}